<?php

use Illuminate\Support\Facades\Route;

use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Auth\RegisterStepController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\MediaController;
use App\Http\Controllers\CodeController;
use App\Http\Controllers\BackendUsersController;
use App\Http\Controllers\WechatUserController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\BackendOrderController;
use App\Http\Controllers\LotteryController;
use App\Http\Controllers\VerificationController;

// 花间村微信公众号（服务号）控制器引入。
use App\Http\Controllers\Wechat\WechatServiceAccount\WechatServiceAccountController;
use App\Http\Controllers\Wechat\WechatServiceAccount\CartController;
use App\Http\Controllers\Wechat\WechatServiceAccount\AddressController;
use App\Http\Controllers\Wechat\WechatServiceAccount\OrderController as WechatOrderController;

Route::get('/wechat/login', [WechatServiceAccountController::class, 'login'])->name('wechat.service.account.login');
Route::get('/wechat/auth', [WechatServiceAccountController::class, 'auth'])->name('wechat.service.account.auth');
Route::get('/wechat/callback', [WechatServiceAccountController::class, 'callback'])->name('wechat.service.account.callback');
Route::get('/wechat/silent-auth', [WechatServiceAccountController::class, 'silentAuth'])->name('wechat.service.account.silent.auth');
Route::get('/wechat/silent-callback', [WechatServiceAccountController::class, 'silentCallback'])->name('wechat.service.account.silent.callback');


/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/greeting', function () {
    return 'Hello World';
});

Auth::routes();

Route::middleware('web')->group(function () {
    Route::post('/register/step1', [RegisterStepController::class, 'step1'])
        ->name('register.step1');
    Route::post('/register/verify-code', [RegisterStepController::class, 'verifyCode'])
        ->name('register.verify-code');
    Route::post('/register/complete', [RegisterStepController::class, 'register'])
        ->name('register.complete');
});

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');



// 商品管理路由
Route::middleware(['auth'])->group(function () {
    Route::get('/codes/search', [CodeController::class, 'search'])
        ->name('codes.search');
    Route::post('/codes/search', [CodeController::class, 'search'])
        ->name('codes.search.post');

    Route::resource('products', ProductController::class);
    Route::patch('/products/{product}/toggle-sale', [ProductController::class, 'toggleSale'])
        ->name('products.toggle-sale');

    Route::post('/products/{product}/media', [MediaController::class, 'store'])->name('media.store');
    Route::delete('/media/{media}', [MediaController::class, 'destroy'])->name('media.destroy');
    Route::resource('codes', CodeController::class);
    // 添加生成二维码的路由
    Route::get('/codes/{id}/qrcode', [CodeController::class, 'downloadQrCode'])->name('codes.qrcode');
    // 兑换码管理相关路由已整合到resource中

    // 后台用户管理路由
    Route::middleware(['auth', 'admin'])->group(function () {
        Route::resource('users', BackendUsersController::class);
        Route::patch('users/{user}/permission', [BackendUsersController::class, 'updatePermission'])
            ->name('users.update-permission');
    });
});

// 微信用户管理路由
Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('/wechat/users', [WechatUserController::class, 'index'])
        ->name('wechat.users.index');
    Route::get('/wechat/users/statistics', [WechatUserController::class, 'statistics'])
        ->name('wechat.users.statistics');
});

// 订单管理路由
Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('/orders', [BackendOrderController::class, 'index'])->name('orders.index');
    Route::get('/orders/statistics', [BackendOrderController::class, 'statistics'])->name('orders.statistics');
    Route::get('/orders/{order}', [BackendOrderController::class, 'show'])->name('orders.show');
    Route::patch('/orders/{order}/status', [BackendOrderController::class, 'updateStatus'])->name('orders.update-status');
});

// 后台订单管理路由
Route::prefix('backend')->name('backend.')->middleware(['auth', 'admin'])->group(function () {
    Route::get('orders', [BackendOrderController::class, 'index'])->name('orders.index');
    Route::get('orders/{order}', [BackendOrderController::class, 'show'])->name('orders.show');
    Route::put('orders/{order}/status', [BackendOrderController::class, 'updateStatus'])->name('orders.updateStatus');
    Route::get('orders/statistics', [BackendOrderController::class, 'statistics'])->name('orders.statistics');
});

// 摇奖管理路由组
Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    // 添加中奖清单路由 - 放在最前面
    Route::get('lottery/winners-list', [\App\Http\Controllers\LotteryController::class, 'winnersList'])
        ->name('lottery.winners-list');

    // 修改抽奖路由
    Route::post('lottery/draw', [\App\Http\Controllers\LotteryController::class, 'draw'])
        ->name('lottery.draw');

    // 添加保存结果的路由
    Route::post('lottery/save-result', [\App\Http\Controllers\LotteryController::class, 'saveResult'])
        ->name('lottery.save-result');

    // 资源路由放在最后
    Route::resource('lottery', \App\Http\Controllers\LotteryController::class);

    // 添加兑换码查询和绑定路由
    Route::get('verification-codes/search', [\App\Http\Controllers\LotteryController::class, 'searchCode'])
        ->name('verification-codes.search');
    Route::post('lottery/bind-code', [LotteryController::class, 'bindCode'])
        ->name('lottery.bind-code')
        ->middleware('web');
});

Route::post('/verification/send', [VerificationController::class, 'send'])->name('verification.send');
Route::post('/verification/verify', [VerificationController::class, 'verify'])->name('register.verify-code');

Route::get('/about', function () {
    return view('about');
});

// WeChat service account 路由组
Route::prefix('wechat-service-account')->name('wechat.service.account.')->group(function () {
    Route::get('/login', [WechatServiceAccountController::class, 'login'])->name('login');
    Route::get('/auth', [WechatServiceAccountController::class, 'auth'])->name('auth');
    Route::get('/callback', [WechatServiceAccountController::class, 'callback'])->name('callback');
    Route::get('/my', [WechatServiceAccountController::class, 'my'])->name('my')->middleware('auth:wechat');
    Route::get('/admin', [WechatServiceAccountController::class, 'admin'])->name('admin')->middleware('auth:wechat');
    
    // 需要会员权限的路由
    Route::get('/index', [WechatServiceAccountController::class, 'index'])->name('index')->middleware('member.permission');
    Route::get('/product/{id}', [WechatServiceAccountController::class, 'show'])->name('product.show')->middleware('member.permission');

    // 库存管理路由
    Route::get('/inventory', [WechatServiceAccountController::class, 'inventory'])->name('inventory')->middleware('auth:wechat');
    
    // 商品编辑路由
    Route::get('/product/{id}/edit', [WechatServiceAccountController::class, 'editProduct'])->name('product.edit')->middleware('auth:wechat');
    Route::put('/product/{id}', [WechatServiceAccountController::class, 'updateProduct'])->name('product.update')->middleware('auth:wechat');

    // 恢复购物车页面路由，直接渲染视图
    Route::get('/cart', [CartController::class, 'index'])->name('cart')->middleware(['auth:wechat', 'member.permission']);
    Route::post('/cart/add', [CartController::class, 'add'])->name('cart.add')->middleware(['auth:wechat', 'member.permission']);
    Route::post('/cart/update', [CartController::class, 'update'])->name('cart.update')->middleware(['auth:wechat', 'member.permission']);
    Route::post('/cart/remove', [CartController::class, 'remove'])->name('cart.remove')->middleware(['auth:wechat', 'member.permission']);
    Route::post('/cart/empty', [CartController::class, 'empty'])->name('cart.empty')->middleware(['auth:wechat', 'member.permission']);
    Route::get('/checkout', [\App\Http\Controllers\Wechat\WechatServiceAccount\CheckoutController::class, 'show'])->middleware(['auth:wechat', 'member.permission'])->name('checkout');
    Route::post('/checkout', [\App\Http\Controllers\Wechat\WechatServiceAccount\CheckoutController::class, 'submit'])->middleware(['auth:wechat', 'member.permission'])->name('checkout.submit');
    Route::post('/checkout/get-js-config', [\App\Http\Controllers\Wechat\WechatServiceAccount\CheckoutController::class, 'getJsConfig'])->middleware('auth:wechat')->name('checkout.get-js-config');
    Route::post('/logout', [WechatServiceAccountController::class, 'logout'])->name('logout');
    Route::get('/addresses', [AddressController::class, 'index'])->name('addresses')->middleware('auth:wechat');
    Route::get('/addresses/create', [AddressController::class, 'create'])->name('addresses.create')->middleware('auth:wechat');
    Route::post('/addresses', [AddressController::class, 'store'])->name('addresses.store')->middleware('auth:wechat');
    Route::get('/addresses/{id}/edit', [AddressController::class, 'edit'])->name('addresses.edit')->middleware('auth:wechat');
    Route::put('/addresses/{id}', [AddressController::class, 'update'])->name('addresses.update')->middleware('auth:wechat');
    Route::delete('/addresses/{id}', [AddressController::class, 'destroy'])->name('addresses.destroy')->middleware('auth:wechat');
    Route::post('/addresses/{id}/set-default', [AddressController::class, 'setDefault'])->name('addresses.set-default')->middleware('auth:wechat');
    // 订单相关路由
    Route::get('/orders', [WechatServiceAccountController::class, 'listOrders'])
        ->name('orders.list')
        ->middleware(['auth:wechat', 'member.permission']);
    Route::get('/orders/{order}', [WechatServiceAccountController::class, 'showOrder'])
        ->name('orders.show')
        ->middleware(['auth:wechat', 'member.permission']);
    Route::post('/orders/{order}/ship', [WechatServiceAccountController::class, 'shipOrder'])
        ->name('orders.ship')
        ->middleware(['auth:wechat', 'member.permission']);
    Route::post('/orders/{order}/cancel', [WechatOrderController::class, 'cancel'])
        ->name('orders.cancel')
        ->middleware(['auth:wechat', 'member.permission']);
    Route::get('/orders/{order}/payment-status', [WechatOrderController::class, 'checkPaymentStatus'])
          ->name('orders.payment-status')
          ->middleware(['auth:wechat', 'member.permission']);
    
    // 卡包相关路由
    Route::get('/wallet', [WechatServiceAccountController::class, 'wallet'])
        ->name('wallet')
        ->middleware(['auth:wechat', 'member.permission']);
    
    // 兑换码相关路由
    Route::get('/redeem', [WechatServiceAccountController::class, 'redeem'])
        ->name('redeem');
    Route::post('/redeem/submit', [WechatServiceAccountController::class, 'redeemSubmit'])
        ->name('redeem.submit')
        ->middleware(['auth:wechat', 'member.permission']);
});